package cn.com.bluemoon.daps.api.model.factory;

import cn.com.bluemoon.daps.api.model.RemoteDataModelRecordService;
import cn.com.bluemoon.daps.common.constant.ServiceNameConstants;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.common.toolkit.FeignClientTools;
import cn.com.bluemoon.daps.model.entity.DapDataModelRecord;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Optional;

@Component
public class RemoteDataModelRecordServiceFallbackFactory implements FallbackFactory<RemoteDataModelRecordService> {

    private static final Logger log = LoggerFactory.getLogger(RemoteDataModelServiceFallbackFactory.class);

    @Override
    public RemoteDataModelRecordService create(Throwable throwable) {
        log.error("模型记录服务调用失败:{}", throwable.getMessage());
        return new RemoteDataModelRecordService() {
            @Override
            public Optional<LocalDateTime> findMaxReleaseTimeBySystemId(String systemId) {
                return Optional.empty();
            }

            @Override
            public ResultBean<DapDataModelRecord> existsRecordByModelId(String modelId) {
                return FeignClientTools.INSTANCE.errorMsgCatch(throwable, ServiceNameConstants.DapsApiModel.PROD_SERVICE, "获取模型发布记录失败");
            }
        };
    }
}
